package cn.cms.util;

import com.google.gson.*;

/**
 * Created by wangdi5 on 2015/3/24.
 */
public final class JSONUtils {


    public static String bean2Json(Object obj){
        Gson gson = new GsonBuilder().create();
        return gson.toJson(obj);
    }

    public static String bean2JsonWithoutNull(Object obj){
        Gson gson = new GsonBuilder().serializeNulls().create();
        return gson.toJson(obj);
    }


    public static <T> T json2Bean(String jsonStr,Class<T> objClass){
        Gson gson = new GsonBuilder().create();
        return gson.fromJson(jsonStr, objClass);
    }
    /**
     * 美化json
     * @param jsonStr
     * @return
     */
    public static String jsonFormatter(String jsonStr){
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        JsonParser jp = new JsonParser();
        JsonElement je = jp.parse(jsonStr);
        String prettyJsonString = gson.toJson(je);
        return prettyJsonString;
    }


}
